package com.example.test

import android.content.Intent
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v4.app.FragmentManager
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.widget.ImageView
import com.zee.base.IBindView

import com.zee.utils.UIUtils
import com.zee.utils.ZScreenUtils


/**
 * @author Administrator
 * @author Administrator
 */
abstract class BaseDialogFragment : DialogFragment(), IBindView {
    protected var rootView: View? = null
        private set
    private var isNoBackGround = false
    private var isBackViewTransparent = false

    private var cancel: Boolean = false
    private var mWidth: Int = 0
    private var mHeight: Int = 0
    private var startY: Float = 0.toFloat()
    private var type: Int = 0

    val isShowing: Boolean
        get() = isAdded


    open fun isAutoScreenChange(): Boolean {
        return true
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView = inflater!!.inflate(layoutResID, container, false)
        setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Holo_Light)


        val bundle = arguments
        if (bundle != null) {
            startY = bundle.getFloat("y", -1f)
            if (startY > -1) {
                val window = dialog.window
                val wlp = window.attributes
                wlp.width = ZScreenUtils.getScreenWidth()
                wlp.height = WindowManager.LayoutParams.WRAP_CONTENT

                wlp.gravity = Gravity.BOTTOM
                wlp.y = startY.toInt()
                window.attributes = wlp
            }
        }
        if (isNoBackGround) {
            dialog.window.setDimAmount(0.1f)
        }
        if (isBackViewTransparent) {
            dialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews(savedInstanceState)
        initDatas()
        otherViewInit()
        dialog?.setCanceledOnTouchOutside(cancel)
        isCancelable = cancel
        val imageView: ImageView = ImageView(context);


    }

    override fun onResume() {
        super.onResume()
        val window = dialog!!.window
        if (window != null && mWidth > 0) {
            window.setLayout(mWidth, mHeight)
        }
    }

    //true 点击屏幕外可以消息,默认是false
    fun setCanceledOnTouchOutside(flag: Boolean) {
        cancel = flag
    }

    protected fun otherViewInit() {}

    //设置背景为空
    fun setDialogBackgroundNull() {
        isNoBackGround = true
    }


    fun setBackgroundViewTransparent() {
        isBackViewTransparent = true
    }

    fun show(manager: FragmentManager) {
        val ft = manager.beginTransaction()
        ft.add(this, "" + layoutResID)
        ft.commitAllowingStateLoss()
    }

    override fun <T : View?> findViewById(id: Int): T {
        return rootView!!.findViewById<View>(id) as T
    }

    /**
     * 初始化视图
     */
    protected abstract fun initViews()

    /**
     * 初始化数据
     */
    protected fun initDatas() {}

    fun setPXSize(width: Int, height: Int) {
        mWidth = width
        mHeight = height
    }

    fun setSize(width: Int, height: Int) {
        mWidth = UIUtils.dpToPx(width)
        mHeight = UIUtils.dpToPx(height)
    }


    override fun dismiss() {
        if (isResumed) {
            super.dismiss()
        }
    }


    protected fun setShowBottomScreen() {//宽度全屏并显示在底部
        setFullWithScreen(0)
    }

    /**
     * 显示在顶部
     */
    fun setTopScreen() {
        setFullWithScreen(3)
    }

    /**
     * 全屏
     */
    fun setFullScreen() {

        setFullWithScreen(1)
    }

    fun setShowType2() {
        setFullWithScreen(2)
    }

    private fun setFullWithScreen(type: Int) {//宽度全屏并显示在底部
        this.type = type
        if (dialog == null) {
            return
        }
        val window = dialog.window
        if (type == 3) {
            window?.setGravity(Gravity.TOP)
        } else {
            window?.setGravity(Gravity.BOTTOM)
        }
        //消除边距
        window.decorView.setPadding(0, 0, 0, 0)
        val lp = window.attributes
        // 现在的方向是横屏显示
        if (isAutoScreenChange() && activity?.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            lp.x = ZScreenUtils.getScreenHeight() - lp.width
            lp.height = WindowManager.LayoutParams.MATCH_PARENT
        } else {
            //设置宽度充满屏幕
            lp.width = WindowManager.LayoutParams.MATCH_PARENT
            if (type == 0 || type == 3) {
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT
            } else {
                lp.height = WindowManager.LayoutParams.MATCH_PARENT
            }
        }

        window.attributes = lp
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }


    fun startActivity(tcVideoRecordActivityClass: Class<*>) {
        val intent = Intent(activity, tcVideoRecordActivityClass)
        activity?.startActivity(intent)
    }
//    operator fun BaseDialogFragment.get(position: Int): String = sections(position)
//
//     fun sections(position: Int): String{
//         return  "";
//     }

}
